<?php
/**
 *
 * UCModApp class file
 *
 * @category   UC
 * @author     刘海涛 <liuhaitao01@snda.com>
 * @copyright  1997-2010 SDO Group
 * @version    Release: 1.0.0
 * @link       http://www.sdo.com
 * @license    http://www.sdo.com
 */
defined('DB_DEV_SERVER') || define('DB_DEV_SERVER',false);

require_once(dirname(__FILE__).'/../../config/UCConfApp.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCache.php');
require_once(dirname(__FILE__).'/../../../op/interface/SdoAppStore.php');

class UCModApp
{
	private $_user_id = '';
	private $_cache = '';
	private $_key = '';
	public	$error_no = 0;
	
	/**
	 * 构造函数
	 *
	 */
	function __construct($user_id)
	{
		if(empty($user_id))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		$this->_user_id = $user_id;
		//$this->_cache	= new UCLibCache(UCConfApp::APP_MEMCACHE_FLAG); 
		//$this->_key		= UCConfApp::APP_MEMCACHE_USER_KEY.$this->_user_id;
	}
	
	/**
	 * 析构函数
	 *
	 */
	function __destruct()
	{
	}

	/**
	 * 获取用户左边栏APP
	 * @param	mix		$app_ids	int | array
	 * @return	array				用户APP数组
	 *
	 */
	function addUserApp($app_ids)
	{
		if(empty($app_ids))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		if(!SdoAppStore::addUserApps($this->_user_id,$app_ids))
		{
			$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
			return false;
		}
		return true;
	}
	

	/**
	 * 获取用户邀请推荐APP
	 * @return	array			用户APP数组
	 *
	 */
	function getUserAppForInvite()
	{	
		$ar_app = SdoAppStore::getUserApps($this->_user_id);
		if(false===$ar_app)
		{
			return false;
		}
		
		$num = 0;
		if(!empty($ar_app))
		{
			foreach($ar_app as $key=>$value)
			{
				if(!array_key_exists($key,UCConfApp::$ar_system_info))
				{
					$ar_ret[$key] = $value;
					$num++;
				}
				if($num>=UCConfApp::APP_INVITE_SHOW_COUNT)
				{
					break;
				}
			}
		}

		if($num<UCConfApp::APP_INVITE_SHOW_COUNT)
		{
			$ar_tmp = array_rand(UCConfApp::$ar_default_info,($num-UCConfApp::APP_INVITE_SHOW_COUNT));
			$ar_default = SdoAppStore::getBatchAppInfo($ar_tmp);
			if(false===$ar_ret)
			{
				$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
				return false;
			}
			$ar_ret = array_merge($ar_ret,$ar_default);
		}
		//return $ar_ret;
		return SdoAppStore::getUserApps($this->_user_id,UCConfApp::APP_INVITE_SHOW_COUNT);
	}

	/**
	 * 获取用户所有APP
	 * @return	array			用户APP数组
	 *
	 */
	function getUserApp($count=0)
	{	
		$ar_app = SdoAppStore::getUserApps($this->_user_id);
		if(false===$ar_app)
		{
			$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
			return false;
		}

		$ar_tmp = array();
		foreach(UCConfApp::$ar_system_info as $value)
		{
			if(!array_key_exists($value,$ar_app))
			{
				$ar_tmp[] = $value;
			}
		}

		if(!empty($ar_tmp))
		{
			if(false===$this->addUserApp($ar_tmp))
			{
				return false;
			}
			
			$ar_app = SdoAppStore::getUserApps($this->_user_id);
			if(false===$ar_app)
			{
				$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
				return false;
			}
		}

		foreach(UCConfApp::$ar_default_info as $value)
		{
			if(!array_key_exists($value,$ar_app))
			{
				$ar_default[] = $value;
			}
		}
		if(!empty($ar_default))
		{
			$ar_default_list = SdoAppStore::getBatchAppInfo($ar_default);
			if(false===$ar_default_list)
			{
				$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
				return false;
			}
			$ar_app = array_merge($ar_app,$ar_default_list);
		}
		
		if(!empty($count))
		{
			$ar_ret = array_slice($ar_app,0,$count);
		}
		else
		{
			$ar_ret = $ar_app;
		}
		return $ar_ret;
	}

	/**
	 * 修改用户APP展示顺序
	 * @param	array	$ar_sequence	序列数组
	 * @return	bool
	 *
	 */
	function modAppSequence($ar_sequence)
	{
		if(empty($ar_sequence))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		$ret = SdoAppStore::setUserAppsPosition($this->_user_id,$ar_sequence);
		if(false===$ret)
		{
			$this->error_no = UCConfApp::APP_ERROR_INTERFACE;
			return false;
		}
		return true;
	}

	/**
	 * 删除用户APP
	 * @param	int		$app_id	APPID
	 * @return	bool
	 *
	 */
	function deleteApp($app_id)
	{
	}

	/**
     * 获取应用的邀请链接
     * 
     * @param int	$appId
     * @param int	$inviterUid 邀请者uid
     * @param bool	$outer		是否是站外邀请
     * @return		string		应用不存在或无效时，返回空字符串，否则返回邀请链接，
     * 站外邀请链接将返回的url做为回跳参数加在注册链接后
     */

	function getAppInviteUrl($app_id,$outer=false)
	{
		if(empty($app_id))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		return SdoAppStore::getAppInviteUrl($app_id,$this->_user_id,$outer);
	}
	
	/**
	 * 获取单个APP信息
	 * @param	int		$app_id	APPID
	 * @return	array			不存在返回NULL
	 *
	 */
	function getUserFriendsInApp($app_id)
	{
		if(empty($app_id))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		return SdoAppStore::getUserFriendsInApp($this->_user_id, $appId);
	}
	
	/**
	 * 获取单个APP信息
	 * @param	int		$app_id	APPID
	 * @return	array			不存在返回NULL
	 *
	 */
	static function getAppInfo($app_id)
	{
		if(empty($app_id))
		{
			$this->error_no = UCConfApp::APP_ERROR_PARAM;
			return false;
		}
		$ar_app = SdoAppStore::getAppInfo($app_id);
		return $ar_app;
	}
	
	 /**
     * 返回所有应用
     * @return array
     */
	static function getAllApps()
	{
		return SdoAppStore::getAllApps();
	}
	
	/**
     * 判断用户是否对一个应用授权过
     *
     * @param	int		$app_id
     * @return	bool
     */
    function isUserApp($app_id)
    {
		return SdoAppStore::isUserApp($this->_user_id, $app_id);
    }
}
?>